package states;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Polygon;
import physics.Block;
import physics.BlockMap;

public class Game extends BasicGame {

    private float playerX = 100;
    private float playerY = 20;
    private Animation player;
    private Animation playerR;
    private Animation playerJumpR;
    private Animation playerL;
    private Animation playerJumpL;
    private Animation playerRR;
    private Animation playerRL;
    private Polygon playerPoly;
    public BlockMap map;
    private Timer fall;
    private boolean jump = false;
    private boolean left = false;
    private boolean run = false;
    private int cnt;
    private boolean isSaltable = true;
    private float[] pos = new float[2];

    public Game() {
        super("one class barebone game");
    }

    public void init(GameContainer container) throws SlickException {
        container.setVSync(true);
        container.setUpdateOnlyWhenVisible(true);
        container.setTargetFrameRate(60);
        container.setMaximumLogicUpdateInterval(8);
        container.setMinimumLogicUpdateInterval(8);
        map = new BlockMap("resources/mapa02.tmx");
        player = new Animation();
        playerR = new Animation();
        playerR.setAutoUpdate(true);
        playerL = new Animation();
        playerL.setAutoUpdate(true);
        playerJumpR = new Animation();
        playerJumpL = new Animation();
        playerRR = new Animation();
        playerRL = new Animation();

        for (int frame = 0; frame < 4; frame++) {
            playerR.addFrame(new Image("resources/RoSta0" + (frame + 1) + ".png"), 200);
            playerL.addFrame(new Image("resources/RoSta11.png"), 200);
            playerJumpR.addFrame(new Image("resources/RoJum01.png"), 200);
            playerJumpL.addFrame(new Image("resources/RoJum11.png"), 200);
            playerRR.addFrame(new Image("resources/RoRun01.png"), 200);
            playerRL.addFrame(new Image("resources/RoRun11.png"), 200);
        }
        playerPoly = new Polygon(new float[]{
                    playerX + 25, playerY + 27,
                    playerX + 45, playerY + 27,
                    playerX + 45, playerY + 71,
                    playerX + 25, playerY + 71
                });
        changeAnimation("nor");
        fall(container);
    }

    public void update(GameContainer container, int delta) throws SlickException {
        if (container.getInput().isKeyDown(Input.KEY_LEFT)) {
            playerX--;
            playerPoly.setX(playerX + 25);
            left=true;
            run=true;
            if (entityCollisionWith()) {
                playerX++;
                playerPoly.setX(playerX + 25);
            }
        }if (container.getInput().isKeyDown(Input.KEY_RIGHT)) {

            playerX++;
            playerPoly.setX(playerX + 25);
            left=false;
            run=true;
            if (entityCollisionWith()) {
                playerX--;
                playerPoly.setX(playerX + 25);
            }
        }if (container.getInput().isKeyPressed(Input.KEY_UP)) {
            if (isSaltable) {
                jump = true;
                isSaltable = false;
                changeAnimation("jump");
            }
        }

    }

    public boolean entityCollisionWith() throws SlickException {
        for (int i = 0; i < BlockMap.entities.size(); i++) {
            Block entity1 = (Block) BlockMap.entities.get(i);
            if (playerPoly.intersects(entity1.poly)) {
                return true;
            }
        }
        return false;
    }

    public void posicionarCamara() {

        if (playerX > 400 / 2 - 40) {
            pos[0] = -(playerX - (400 / 2 - 35));
        } else {
            pos[0] = 0;
        }
        if (playerY > 200 / 2 - 47) {
            pos[1] = -(playerY - (200 / 2 - 47));

        } else {
            pos[1] = 0;
        }


    }

    public void render(GameContainer container, Graphics g) {
        posicionarCamara();
        BlockMap.tmap.render((int) pos[0], (int) pos[1]);
        g.drawAnimation(player, playerX + (int) pos[0], playerY + (int) pos[1]);
        //g.draw(playerPoly);

    }

    public static void main(String[] argv) throws SlickException {
        AppGameContainer container =
                new AppGameContainer(new Game(), 400, 200, false);
        container.start();
    }

    public void fall(final GameContainer container) {
        fall = new Timer(5, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //fall.stop();
                if (jump == false) {
                    playerY++;
                    playerPoly.setY(playerY + 27);
                    changeAnimation("jump");
                    try {
                        if (entityCollisionWith()) {
                            playerY--;
                            playerPoly.setY(playerY + 27);
                            isSaltable = true;
                            changeAnimation("nor");
                            if(!(container.getInput().isKeyDown(Input.KEY_RIGHT)==true || container.getInput().isKeyDown(Input.KEY_LEFT)==true)){
                                run=false;
                            }
                        }
                    } catch (SlickException ex) {
                        System.out.println(ex);
                    }
                } else {
                    playerY--;
                    playerPoly.setY(playerY + 27);
                    try {
                        if (entityCollisionWith()) {
                            playerY++;
                            playerPoly.setY(playerY + 27);
                            jump = false;

                        }

                    } catch (SlickException ex) {
                        System.out.println(ex);
                    }
                    cnt++;

                    if (cnt > 55) {
                        cnt = 0;
                        jump = false;
                    }
                }
            }
        });
        this.fall.start();
    }



    public void changeAnimation(String ani) {
        if (ani.equals("jump") && left == false) {
            player = playerJumpR;
        }
        if (ani.equals("jump") && left == true) {
            player = playerJumpL;
        }
        if (ani.equals("nor") && left == true) {
            player = playerL;
        }
        if (ani.equals("nor") && left == false) {
            player = playerR;
        }
        if (ani.equals("nor") && run==true && left == true && jump==false) {
            player = playerRL;
        }
        if (ani.equals("nor") && run==true && left == false && jump==false) {
            player = playerRR;
        }
    }
}
